#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
#
# 2018.05.27
# 1.0.7.test

OutputLOG=/etc/inexistence/01.Log/install_libtorrent_rasterbar.txt # /dev/null
DebLocation=/etc/inexistence/01.Log/DebPackages
SCLocation=/etc/inexistence/01.Log/SourceCodes
LOCKLocation=/etc/inexistence/01.Log/Lock

cat>>$OutputLOG<<EOF

$(date "+%Y.%m.%d.%H.%M.%S")

EOF

# Colors
black=$(tput setaf 0); red=$(tput setaf 1); green=$(tput setaf 2); yellow=$(tput setaf 3); blue=$(tput setaf 4); magenta=$(tput setaf 5); cyan=$(tput setaf 6); white=$(tput setaf 7);
on_red=$(tput setab 1); on_green=$(tput setab 2); on_yellow=$(tput setab 3); on_blue=$(tput setab 4); on_magenta=$(tput setab 5); on_cyan=$(tput setab 6); on_white=$(tput setab 7); bold=$(tput bold);
dim=$(tput dim); underline=$(tput smul); reset_underline=$(tput rmul); standout=$(tput smso); reset_standout=$(tput rmso); normal=$(tput sgr0); alert=${white}${on_red}; title=${standout};
baihuangse=${white}${on_yellow}; bailanse=${white}${on_blue}; bailvse=${white}${on_green}; baiqingse=${white}${on_cyan}; baihongse=${white}${on_red}; baizise=${white}${on_magenta};
heibaise=${black}${on_white}; heihuangse=${on_yellow}${black}; jiacu=${normal}${bold}; shanshuo=$(tput blink); wuguangbiao=$(tput civis); guangbiao=$(tput cnorm)
CW="${bold}${baihongse} ERROR ${jiacu}";ZY="${baihongse}${bold} ATTENTION ${jiacu}";JG="${baihongse}${bold} WARNING ${jiacu}"

# Sustem check
CODENAME=`  cat /etc/os-release | grep VERSION= | tr '[A-Z]' '[a-z]' | sed 's/\"\|(\|)\|[0-9.,]\|version\|lts//g' | awk '{print $2}'  `

# Get options
OPTS=$(getopt -n "$0" -o m:v:b: --long "install-mode:,version:,branch:" -- "$@")

eval set -- "$OPTS"

while true; do
  case "$1" in
    -m | --install-mode ) mode="$2"     ; shift ; shift ;;
    -v | --version      ) version="$2"  ; shift ; shift ;;
    -b | --branch       ) branch="$2"   ; shift ; shift ;;
     * ) break ;;
  esac
done

case $mode in
    apt     ) sleep 0  ;;
    deb     ) sleep 0  ;;
    compile ) sleep 0  ;;
    ""   | *) echo -e "\n${CW} Install mode must be specified as apt, deb or compile${normal}\n"   ;;
esac


[[ -z $MAXCPUS ]] && MAXCPUS=$(nproc)
# Transform version to branch
[[ ` echo $version | grep -Eo "[012]\.[0-9]+\.[0-9]+" ` ]] && branch=` echo libtorrent-$version | sed "s/\./_/g" `
# Check if input branch exists
[[ ! -z $branch ]] && [[ ! ` wget -qO- "https://github.com/arvidn/libtorrent" | grep data-name | cut -d '"' -f2 | grep -P "$branch\b" ` ]] && { echo -e "\n${CW} No such branch!\n" ; exit 1 ; }
# Use RC_1_0 if not specified
[[ -z $branch ]] && branch=RC_1_0
# Determin RC_1_0 and RC_1_1's version
[[ $branch == RC_1_0 ]] && version=1.0.11 ; [[ $branch == RC_1_1 ]] && version=`  wget -qO- "https://github.com/arvidn/libtorrent" | grep "data-name" | cut -d '"' -f2 | grep "libtorrent-1_1_" | sort -t _ -n -k 3 | tail -n1 | sed "s/_/\./g" | sed "s/libtorrent-//"  `
# Use 6.6.6 when version cannot be determind
[ ! -z $branch ] && [ -z $version ] && version=6.6.6



# Install dependency
function _install_lt_dependency() {
apt-get install -y                                                                  \
build-essential pkg-config autoconf automake libtool git checkinstall               \
libboost-dev libboost-system-dev libboost-chrono-dev libboost-random-dev libssl-dev \
geoip-database libgeoip-dev                                                         \
libboost-python-dev                                                                 \
zlib1g-dev >> $OutputLOG 2>&1 ; }



# Install from source code
function _install_lt_compile() {

echo -e "Install from source code \nbracnh $branch, version $version\n" >> $OutputLOG 2>&1
git clone --depth=1 -b $branch --single-branch https://github.com/arvidn/libtorrent libtorrent-$version >> $OutputLOG 2>&1
cd libtorrent-$version

# https://github.com/qbittorrent/qBittorrent/issues/6383
sed -i "s/+ target_specific(),/+ target_specific() + ['-std=c++11'],/" bindings/python/setup.py

./autotool.sh >> $OutputLOG 2>&1
./configure --enable-python-binding --with-libiconv \
            --disable-debug --enable-encryption --with-libgeoip=system CXXFLAGS=-std=c++11  >> $OutputLOG 2>&1 # For both Deluge and qBittorrent
make -j$MAXCPUS >> $OutputLOG 2>&1

mkdir -p doc-pak
cat >description-pak<<EOF
an efficient feature complete C++ bittorrent implementation
EOF

# make install
checkinstall -y --pkgname=libtorrent-rasterbar-seedbox --pkggroup libtorrent --pkgversion $version >> $OutputLOG 2>&1

cp -f libtorrent-rasterb*.deb $DebLocation
touch $LOCKLocation/libtorrent-rasterbar.lock
touch $LOCKLocation/libtorrent-rasterbar.compile.$branch.lock ; }



# Install from pre-compiled deb package
function _install_lt_deb() {

echo -e "Install from pre-compiled deb package\n" >> $OutputLOG 2>&1

if [[ $CODENAME == jessie ]]; then
    wget -O lt.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Deb%20Package/Jessie/libtorrent-rasterbar_1.0.11_jessie_amd64.deb         >> $OutputLOG 2>&1
elif [[ $CODENAME == xenial ]]; then
    wget -O lt.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Deb%20Package/Xenial/libtorrent-rasterbar_1.0.11_xenial_amd64.deb         >> $OutputLOG 2>&1
elif [[ $CODENAME == stretch ]]; then
    wget -O lt.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Deb%20Package/Stretch/libtorrent-rasterbar_1.0.11_stretch_amd64.deb       >> $OutputLOG 2>&1
elif [[ $CODENAME == bionic ]]; then
    wget -O lt.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Deb%20Package/Bionic/libtorrent-rasterbar-seedbox_1.0.11_bionic_amd64.deb >> $OutputLOG 2>&1
fi

dpkg -i lt.deb >> $OutputLOG 2>&1
touch $LOCKLocation/libtorrent-rasterbar.lock
touch $LOCKLocation/libtorrent-rasterbar.deb.lock ; }



# Install from repository
function _install_lt_apt() { echo -e "Install from repository\n" >> $OutputLOG 2>&1
apt-get -y install libtorrent-rasterbar-dev python-libtorrent    >> $OutputLOG 2>&1
touch $LOCKLocation/libtorrent-rasterbar.lock
touch $LOCKLocation/libtorrent-rasterbar.apt.lock ; }

mkdir -p $SCLocation $DebLocation $LOCKLocation
cd       $SCLocation

case $mode in
    apt     ) _install_lt_apt                                ;;
    deb     ) _install_lt_dependency && _install_lt_deb      ;;
    compile ) _install_lt_dependency && _install_lt_compile  ;;
esac

cd ; ldconfig
